package org.andes.kownledge.operator.executable;

import java.util.ArrayList;
import java.util.List;

import org.andes.kownledge.operator.Executable;
import org.andes.kownledge.operator.Predication;
import org.andes.kownledge.operator.Term;
import org.andes.kownledge.operator.Variable;
import org.andes.sgg.Qsolver;
import org.andes.sgg.st;

public class SetOf extends Executable {

	
	/// This executable is (setof <Goal> <Term> <SetVar>) where <Setvar>
	/// is a ?-prefixed variable, <Term> is a term and <Goal> is an atomic
	/// proposition.  Goal must include Term.  Setof finds all possible
	/// proofs of Goal, then puts ground versions of <Term>, one for each
	/// state, into SetVar's value.  Setof returns a set consisting of
	/// single state.  The state is a copy of the given state, except that
	/// SetVar is bound to the set, and the working memories and histories
	/// of the derivations are merged into its wm and history.  The path
	/// is strange (see setof-paths). For instance,

	// (setof (vector ?b (force ?b . ?rest) ?dir)
	//        (force ?b . ?rest) 
	//        ?forces)

	/// will generate all force vectors for ?b, and return a state which
	/// contains those derivations in its working memory and history, plus
	/// a binding of ?forces to the set of force terms.

//	(defun execute-setof (Ex State)
//			  "Executes the setof precond executable and returns a set containing a single state"
//			  (let* ((Goal (second Ex))
//				 (Term (third Ex))
//				 (SetVar (fourth Ex))
//				 (Initial-state (copy-st State))
//				 (Result))
//			    (setf (st-stack Initial-state) (list Goal))
//			    (dolist (SubState (solution-sts initial-state))
//			      (setf (st-wm State)
//			    		  (union (st-wm SubState) (st-wm State) :test #'unify))
//			      (setf (st-history State)
//			    		  (union (st-history SubState) (st-history State) :test #'unify))
//			      (pushnew (subst-bindings (st-bindings SubState) Term)
//				       Result :test #'unify))
//			    (push-binding SetVar Result State)
//			;; The history is long and not in tree form
//			;; However, entries are used by the Helpsystem
//			    (setof-actions state initial-state)
//			;; Show the end result; see removable-actionp
//			    (push (list 'setof-result Result) (st-actions State))
//			    (list State)))
	@Override 
	public List<st> exec(st state) {
		st is=state.copy();
		is.getStack().push(Goal);
		List<Term> result=new ArrayList<Term>();
		for(st SubState:Qsolver.instance.solution_sts(is)){
			state.getWm().addAll(SubState.getWm());//:test #'unify
			state.getHistory().addAll(SubState.getHistory());
			result.add(term.substBindings(SubState.getBindings()));
		}
		state.getBindings().extend_bindings(VarSet, result);
		state.setofAction(is);
		List<st> returnst=new ArrayList<st>();
		returnst.add(state);
		return returnst;
	}
	
	private Term Goal;
	public void setGoal(Term Goal1){
		Goal=Goal1;
	}
	public Term getGoal(){
		return Goal;
	}
	private Term term;
	public void setTerm(Term term){
		this.term=term;
	}
	public Term getTerm(){
		return term;
	}
	private Variable VarSet;;
	
	
	public void setVarSet(Variable varSet2) {
		VarSet=varSet2;
		
	}

}
